package com.fr.web.core.service;

import com.fr.base.ColumnRow;
import com.fr.base.core.json.JSONObject;
import com.fr.report.Report;
import com.fr.report.ResultReport;
import com.fr.report.ResultWorkBook;
import com.fr.report.core.FormException;
import com.fr.report.core.FormReport;
import com.fr.web.core.SessionIDInfor;
import com.fr.web.core.WebUtils;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public abstract class InsertUpdateService
{
  public static void insertRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    smartInsertRecord(paramSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse, false);
  }

  public static void simpleInsertRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    smartInsertRecord(paramSessionIDInfor, paramHttpServletRequest, paramHttpServletResponse, true);
  }

  public static void smartInsertRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse, boolean paramBoolean)
    throws Exception
  {
    int i = 0;
    int j = 0;
    int k = 0;
    int l = 0;
    if (paramBoolean)
    {
      k = Integer.parseInt(paramHttpServletRequest.getParameter("ccols"));
      l = Integer.parseInt(paramHttpServletRequest.getParameter("crows"));
    }
    else
    {
      i = Integer.parseInt(paramHttpServletRequest.getParameter("ccolumn"));
      j = Integer.parseInt(paramHttpServletRequest.getParameter("crow"));
    }
    int i1 = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    boolean bool = Boolean.valueOf(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "beforePoint")).booleanValue();
    int i2 = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "count"));
    ResultWorkBook localResultWorkBook = paramSessionIDInfor.getWorkBook2Show();
    ResultReport localResultReport = localResultWorkBook.getResultReport(i1);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject1 = new JSONObject();
    if (localResultReport instanceof FormReport)
      try
      {
        ColumnRow localColumnRow = (paramBoolean) ? ((FormReport)localResultReport).simpleAppendData(ColumnRow.valueOf(k, l), bool, i2) : ((FormReport)localResultReport).appendData(ColumnRow.valueOf(i, j), bool, i2);
        localResultReport.recalculate(paramSessionIDInfor.getParameterMap4Execute());
        JSONObject localJSONObject2 = new JSONObject();
        localJSONObject2.put("col", localColumnRow.column);
        localJSONObject2.put("row", localColumnRow.row);
        localJSONObject1.put("fec", localJSONObject2);
      }
      catch (Exception localException)
      {
        localJSONObject1.put("exception", localException.getMessage());
      }
    localJSONObject1.write(localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static void smartDeleteRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    int j = Integer.parseInt(paramHttpServletRequest.getParameter("ccolumn"));
    int k = Integer.parseInt(paramHttpServletRequest.getParameter("crow"));
    ResultWorkBook localResultWorkBook = paramSessionIDInfor.getWorkBook2Show();
    Report localReport = localResultWorkBook.getReport(i);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    if (localReport instanceof FormReport)
      try
      {
        ((FormReport)localReport).deleteData(ColumnRow.valueOf(j, k));
      }
      catch (FormException localFormException)
      {
        localJSONObject.put("exception", localFormException.getMessage());
      }
    localJSONObject.write(localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
  }

  public static void simpleDeleteRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    String str1 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "crows");
    String str2 = WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "ccols");
    if ((str1 != null) && (str2 != null))
    {
      int k;
      String[] arrayOfString1 = str1.split(",");
      String[] arrayOfString2 = str2.split(",");
      int j = arrayOfString1.length;
      ResultWorkBook localResultWorkBook = paramSessionIDInfor.getWorkBook2Show();
      Report localReport = localResultWorkBook.getReport(i);
      PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
      JSONObject localJSONObject = new JSONObject();
      if (localReport instanceof FormReport)
        for (k = 0; k < j; ++k)
        {
          ColumnRow localColumnRow = ColumnRow.valueOf(Integer.parseInt(arrayOfString2[k]), Integer.parseInt(arrayOfString1[k]));
          try
          {
            ((FormReport)localReport).simpleDeleteData(localColumnRow);
          }
          catch (FormException localFormException)
          {
            localJSONObject.put("exception", localFormException.getMessage());
          }
        }
      localJSONObject.write(localPrintWriter);
      localPrintWriter.flush();
      localPrintWriter.close();
    }
  }

  public static void smartUnDeleteRecord(SessionIDInfor paramSessionIDInfor, HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws Exception
  {
    int i = Integer.parseInt(WebUtils.getHTTPRequestParameter(paramHttpServletRequest, "reportIndex"));
    int j = Integer.parseInt(paramHttpServletRequest.getParameter("column"));
    int k = Integer.parseInt(paramHttpServletRequest.getParameter("row"));
    ResultWorkBook localResultWorkBook = paramSessionIDInfor.getWorkBook2Show();
    Report localReport = localResultWorkBook.getReport(i);
    PrintWriter localPrintWriter = WebUtils.createPrintWriter(paramHttpServletResponse);
    JSONObject localJSONObject = new JSONObject();
    if (localReport instanceof FormReport)
      try
      {
        ((FormReport)localReport).unDeleteData(ColumnRow.valueOf(j, k));
      }
      catch (FormException localFormException)
      {
        localJSONObject.put("exception", localFormException.getMessage());
      }
    localJSONObject.write(localPrintWriter);
    localPrintWriter.flush();
    localPrintWriter.close();
  }
}